<html>
<head><meta charset="utf-8"><title>Working with slices · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Working.20with.20slices.html">Working with slices</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="215096668"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Working%20with%20slices/near/215096668" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Johannes <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Working.20with.20slices.html#215096668">(Oct 30 2020 at 13:52)</a>:</h4>
<p>I have a function that parses a byte array, it currently looks like this:</p>
<div class="codehilite"><pre><span></span><code>fn parse_status_payload(payload: Vec&lt;u8&gt;) -&gt; Result&lt;Value, Box&lt;dyn std::error::Error&gt;&gt; {
    Ok(json!({
        &quot;SystemTime&quot;:        u64::from_be_bytes(payload.get(0..8).ok_or(&quot;Unexpected payload length&quot;)?.try_into()?),
        &quot;UtcDate&quot;:           u32::from_be_bytes(payload.get(8..12).ok_or(&quot;Unexpected payload length&quot;)?.try_into()?),
        //...
</code></pre></div>


<p>Is there an easier way to do this? this looks kinda ugly</p>



<a name="215097441"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Working%20with%20slices/near/215097441" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Working.20with.20slices.html#215097441">(Oct 30 2020 at 13:58)</a>:</h4>
<p>Could use byteorder, but that looks about right. The input might not be long enough, so you have to handle that, and slicing doesn't preserve the length in the typesystem.</p>



<a name="215097576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Working%20with%20slices/near/215097576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Working.20with.20slices.html#215097576">(Oct 30 2020 at 13:59)</a>:</h4>
<p>Once const generics (maybe not min const generics?) lands, you'd be able to convert the input vec into a <code>&amp;[u8; 12]</code> and then perform all compile-time slicing without risk of error</p>



<a name="215097610"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Working%20with%20slices/near/215097610" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Working.20with.20slices.html#215097610">(Oct 30 2020 at 13:59)</a>:</h4>
<p>reducing it to a single dynamic length check / error</p>



<a name="215097829"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Working%20with%20slices/near/215097829" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Johannes <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Working.20with.20slices.html#215097829">(Oct 30 2020 at 14:01)</a>:</h4>
<p>Hmm ok thank you. I am very new to rust and wasn't sure if I am missing something here.</p>



<a name="215098026"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Working%20with%20slices/near/215098026" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Johannes <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Working.20with.20slices.html#215098026">(Oct 30 2020 at 14:02)</a>:</h4>
<p>It's annoying that a slice doesn't get converted to an array automatically. Also do you know why u32::from_be_bytes() does not accept a slice?</p>



<a name="215098118"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Working%20with%20slices/near/215098118" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Working.20with.20slices.html#215098118">(Oct 30 2020 at 14:03)</a>:</h4>
<p>I don't think it's a great idea to cross post everywhere<br>
without giving time for answers<br>
Copying from Discord:</p>



<a name="215098151"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Working%20with%20slices/near/215098151" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Working.20with.20slices.html#215098151">(Oct 30 2020 at 14:03)</a>:</h4>
<p>I expect that a library will spring into existence (I've thought about writing it myself) that will let you do:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[derive(FromBytesBe)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">X</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">system_time</span>: <span class="kt">u64</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">utc_date</span>: <span class="kt">u32</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="n">X</span>::<span class="n">from_bytes_be</span><span class="p">(</span><span class="o">&amp;</span><span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">12</span><span class="p">])</span><span class="w"></span>
</code></pre></div>



<a name="215098399"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Working%20with%20slices/near/215098399" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Johannes <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Working.20with.20slices.html#215098399">(Oct 30 2020 at 14:05)</a>:</h4>
<p>OK I'm sorry I'll keep that in mind. I wasn't sure how active zulip is. Is discord the main platform?</p>



<a name="215098419"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Working%20with%20slices/near/215098419" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Working.20with.20slices.html#215098419">(Oct 30 2020 at 14:05)</a>:</h4>
<blockquote>
<p>It's annoying that a slice doesn't get converted to an array automatically</p>
</blockquote>
<p>Because what should happen if the slice isn't the right length for the array?</p>
<blockquote>
<p>why u32::from_be_bytes() does not accept a slice</p>
</blockquote>
<p>Because what should happen if the slice isn't the right length for an <code>u32</code>?</p>



<a name="215098546"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Working%20with%20slices/near/215098546" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Working.20with.20slices.html#215098546">(Oct 30 2020 at 14:06)</a>:</h4>
<p>Zulip is roughly people working on Rust. Discord is more people working _with_ Rust, some working _on_ Rust. There's also a community Discord server that's mostly people working with Rust.</p>



<a name="215098605"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Working%20with%20slices/near/215098605" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Working.20with.20slices.html#215098605">(Oct 30 2020 at 14:06)</a>:</h4>
<p>It's fine to ask here, just annoying to have to answer the same question both places simultaneously</p>



<a name="215098775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Working%20with%20slices/near/215098775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Johannes <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Working.20with.20slices.html#215098775">(Oct 30 2020 at 14:08)</a>:</h4>
<p>Yeah makes perfekt sense, thanks.</p>



<a name="215121034"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Working%20with%20slices/near/215121034" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Working.20with.20slices.html#215121034">(Oct 30 2020 at 16:49)</a>:</h4>
<p>Personally, I'd recommend the <a href="https://discord.gg/aVESxV8">community discord</a> for this kind of question.  In my experience it's the most active with people looking to reply to questions like that.</p>



<a name="215125011"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Working%20with%20slices/near/215125011" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Working.20with.20slices.html#215125011">(Oct 30 2020 at 17:20)</a>:</h4>
<p>Activity is roughly in the order: Community Discord, Zulip, Other Platforms (Official Discord, Discourse, etc)</p>



<a name="215125103"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Working%20with%20slices/near/215125103" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oliver <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Working.20with.20slices.html#215125103">(Oct 30 2020 at 17:21)</a>:</h4>
<p>semi-quiet Slack channel as well :)</p>



<a name="215125679"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Working%20with%20slices/near/215125679" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Working.20with.20slices.html#215125679">(Oct 30 2020 at 17:26)</a>:</h4>
<p>even freenode <code>##rust</code> still has quite a few members</p>



<a name="215126108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Working%20with%20slices/near/215126108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oliver <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Working.20with.20slices.html#215126108">(Oct 30 2020 at 17:28)</a>:</h4>
<p>also there are three separate matrix channels, two are official, one is for embedded</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>